#include "storelogin.h"
#include "ui_storelogin.h"
#include <QMessageBox>

StoreLogin::StoreLogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::StoreLogin)
{
    ui->setupUi(this);
    this->myDatabase.getConnection();
}

StoreLogin::~StoreLogin()
{
    delete ui;
}

void StoreLogin::on_Register_clicked()
{
    StoreRegister * pstoreg = new StoreRegister();
    pstoreg->show();
    this->close();
}


void StoreLogin::on_Quit_clicked()
{
    this->~StoreLogin();
}


void StoreLogin::on_Login_clicked()
{
    QString storeInputAccount = ui->Edit_account->text();
    QString storeInputPassword = ui->Edit_password->text();
    //判断用户名和密码是否为空
    if(storeInputAccount==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Account can't be NULL!");
    }
    else if (storeInputPassword==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Password can't be NULL!");
    }
    else
    {
        QSqlQuery sql;

        sql.prepare("SELECT * FROM tb_store WHERE saccount = :storeInputAccount");
        sql.bindValue(":storeInputAccount", storeInputAccount);
        sql.exec();

        bool nFlag = 0;
        QString storeAccount;
        QString storePassword;
        while(sql.next()){
            storeAccount = sql.value("saccount").toString();
            storePassword = sql.value("spassword").toString();
            if(storeInputAccount == storeAccount && storeInputPassword == storePassword){
                int sid = sql.value("sid").toInt();
                StoreView* pStoreView = new StoreView(this,sid);
                qDebug()<<"login sid = "<<sid;
                nFlag = 1;
                this->close();
                this->pMainWindow->close();
                pStoreView->show();
            }
        }
        if(!nFlag)
            QMessageBox::information(this,"提示","登陆失败，请重试！",QMessageBox::Yes);
    }
}

